<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Subject Manager</title>
<script>
function Delete() {
	if (confirm("是否确认删除？")) {
		return true;
	}
	return false;
} 
</script>
<script type="text/javascript">
	//删除信息提示
	 $(function() {
		if ("${msg}" != '') {
			 $("#deleteMsg").css("display","block");
				setTimeout(function(){$("#deleteMsg").css("display","none"); 
				},3000);
		}
	})
	
	$(function(){
	   $(".close").click(function(){
	      $("#myAlert").alert();
	   });
	});  
	//修改信息提示
	$(function() {
		if ("${editMsg}" != '') {
			alert("${editMsg}");
		}
	})
	 //增加信息提示
	$(function() {
		if ("${addMsg}" != '') {
			alert("${addMsg}");
			/* $("#sucMsg").css("display","block");
			setTimeout(function(){
				$("#sucMsg").css("display","none");
			},2000); */
		}
	}) 
	function update() {
		var flag = document.getElementById("edit");
		if (confirm("是否确认修改？")) {
			return true;
		}
		return false;
	}
	
	
	 function Delete() {
		//var flag = document.getElementById("delete");
		if (confirm("是否确认删除？")) {
			return true;
		}
		return false;
	}  
	
</script>
<!-- validate前端校验 -->
<script>
$().ready(function() {
    $("#form1").validate({
    	rules: {
    		"examSubjectName":{
    		 required:true,
    		 rangelength:[1,12]
    		},
    		"examSubjectDescription":{
    			rangelength:[2,10]
    		},
    		"examSubjectTnumber":{
    			required:true,
    			digits:true,
    		},
    		"examSubjectTotaltime":{
    			required:true,
    			digits:true,
    			maxlength:4
    		},
    		"examSubjectSum":{
    			required:true,
    			digits:true,
    			maxlength:4
    		},
    		"examSubjectFlag":{
    			required:true,
    			digits:true
    		},
    		"examSubjectDone":{
    			digits:true,
    			maxlength:2
    		}
	    },
	    messages: {
   		 "examSubjectName": "请输入课程名",
   		 "examSubjectTnumber":"请正确输入总题数（学号为数字格式）",
   		 "examSubjectTotaltime":"请输入考试总时长",
   		 "examSubjectSum":"请输入该科目总分",
   		 "examSubjectFlag":"请选择是否为开放状态",
	    }
    });
});
</script>
<!-- 新增模态框Ajax -->
<script>
	$(function(){
		$("#submit").click(function(){
			if ($("#form1").valid()) {
				var subjectName =$("#examSubjectName").val();
				var subjectDescription = $("#examSubjectDescription").val();
				var subjectNumber = $("#examSubjectTnumber").val();
				var subjectTime= $("#examSubjectTotaltime").val();
				var subjectSum = $("#examSubjectSum").val();
				var subjectFlag = $('input:radio:checked').val();
				var subjectDone = $('input:radio:checked').val();
				$.ajax({
					url:"${pageContext.request.contextPath }/exam/addsubject",
					dataType:"json",
					data:{
						examSubjectName:subjectName,
						examSubjectDescription:subjectDescription,
						examSubjectTnumber:subjectNumber,
						examSubjectTotaltime:subjectTime,
						examSubjectSum:subjectSum,
						examSubjectFlag:subjectFlag,
						examSubjectDone:subjectDone,
					},
					type:"POST",
					async:true,
					success:function(data){
						$("#sucMsg").css("display","block");
						setTimeout(function(){
							$("#sucMsg").css("display","none");
						//alert(66);
						},2000);
						if(setTimeout(function(){
							$("#sucMsg").css("display","none");
						},4000)==true){
							setTimeout(function(){
								$("#myAddModal").modal("hide");
							},4000);
						}
							var url = "${pageContext.request.contextPath }/exam/subject";
							window.location.href=url;
					},
					error:function(data){
						$("#eroMsg").css("display","block");
						setTimeout(function(){$("#eroMsg").css("display","none")},10000);
						alert("增加失败");
					}
				})
			}
		})	
		
		$("#close").click(function(){
			var url = "${pageContext.request.contextPath }/exam/subject";
			window.location.href=url;
		})
	});
</script>
<!-- 编辑模态框Ajax -->
<script>
<!-- 取值 -->
function editSubject(id){
	$.ajax({
		url:"${pageContext.request.contextPath }/exam/editsubject/"+id+"?ajax=true",
		dataType:"json",
		type:"GET",
		async:true,
		success:function(data){
		$.each(data.subjects, function(i, stu) {
		$("#examSubjectId1").val(id);
		$("#examSubjectName1").val(stu.examSubjectName);
		$("#examSubjectDescription1").val(stu.examSubjectDescription);
		$("#examSubjectTnumber1").val(stu.examSubjectTnumber);
		$("#examSubjectTotaltime1").val(stu.examSubjectTotaltime);
		$("#examSubjectSum1").val(stu.examSubjectSum);
		$("#examSubjectDone1").val(stu.examSubjectDone);
		if(stu.examSubjcetFlag=="0"){
			$("#falg2").attr({checked:"checked"});
		}else if(stu.examSubjcetFlag=="1"){
			$("#falg1").attr({checked:"checked"});
		}
		})
		},
		error:function(data){
			alert("error");
		}
	});
}
</script>
<script type="text/javascript">
$(function(){
	$("#submit1").click(function(){
		 if ($("#form1").valid()) {
			var id1 = $("#examSubjectId1").val();
			var name1 =$("#examSubjectName1").val();
			var description1 = $("#examSubjectDescription1").val();
			var number1 = $("#examSubjectTnumber1").val();
			var time1 = $("#examSubjectTotaltime1").val();
			var sum1 = $("#examSubjectSum1").val();
			var flag1 = $('#radioFlag').find('input:radio').eq(0).val();
			var done1 = $("#examSubjectDone2").val();
			alert(done1)
			$.ajax({
				url:"${pageContext.request.contextPath }/exam/editsubject",
				dataType:"json",
				data:{
					examSubjectId:id1,
					examSubjectName:name1,
					examSubjectDescription:description1,
					examSubjectTnumber:number1,
					examSubjectTotaltime:time1,
					examSubjectSum:sum1,
					examSubjectFlag:flag1,
					examSubjectDone:done1
				},
				type:"POST",
				async:true,
				success:function(data){
					$("#editSucMsg").css("display","block");
					setTimeout(function(){
						$("#editSucMsg").css("display","none");
					//alert(66);
					},2000);
					if(setTimeout(function(){
						$("#editSucMsg").css("display","none");
					},4000)==true){
						setTimeout(function(){
							$("#myEditModal").modal("hide");
						},4000);
					}
						var url = "${pageContext.request.contextPath }/exam/subject";
						window.location.href=url;
				},
				error:function(data){
					$("#editEroMsg").css("display","block");
					setTimeout(function(){$("#editEroMsg").css("display","none")},10000);
					alert("修改失败");
				}
			})
		}
	})	
		$("#close").click(function(){
			var url = "${pageContext.request.contextPath }/exam/subject";
			window.location.href=url;
		})
	});
</script>
</head>
<body>
	<table class="table table-hover table-striped table-bordered" 
			cellSpacing="0" cellPadding="0">
			<tr style="text-align: center;" class="info">
				<td>科目Id</td>
				<td>科目名</td>
				<td>科目描述</td>
				<td>科目总题数</td>
				<td>考试总时长</td>
				<td>总分</td>
				<td>是否开放</td>
				<td>是否完成考试</td>
				<td>编辑</td>
				<td>删除</td>
				<td>查询</td>
			</tr>
			<c:forEach items="${subjectList }" var="subjectList">
		
				<tr>
					<td>${subjectList.examSubjectId }</td>
					<td>${subjectList.examSubjectName }</td>
					<td>${subjectList.examSubjectDescription }</td>
					<td>${subjectList.examSubjectTnumber }</td>
					<td>${subjectList.examSubjectTotaltime }</td>
					<td>${subjectList.examSubjectSum }</td>
					<td>
						<c:if test="${subjectList.examSubjectFlag eq '0'}">
							<span class="glyphicon glyphicon-ok"></span>
						</c:if>
						<c:if test="${subjectList.examSubjectFlag eq '1' }">
							<span class="glyphicon glyphicon-remove"></span>
						</c:if>
					</td>
					<td>
						<c:if test="${subjectList.examSubjectDone eq '1'}">
							<span class="glyphicon glyphicon-ok"></span>
						</c:if>
						<c:if test="${subjectList.examSubjectDone eq '0' }">
							<span class="glyphicon glyphicon-remove"></span>
						</c:if>
					</td>
					<td class="danger">
						<span class="glyphicon glyphicon-pencil" aria-hidden="true"
						data-toggle="modal" data-target="#myEditModal" onclick="editSubject(${subjectList.examSubjectId })"></span>
					<td class="danger"><a class="close glyphicon glyphicon-remove" href="${pageContext.request.contextPath }/exam/deletesubject/${subjectList.examSubjectId}" 
						data-dismiss="alert" id="delete" onclick="return Delete()"></a>	
					</td>	
					<td class="danger">
						<a class="glyphicon glyphicon-search" aria-hidden="true" 
						href="${pageContext.request.contextPath }/exam/searchGradeById/${subjectList.examSubjectId}"></a>
					</td>
				</tr>
			</c:forEach>
			
						<!-- 新增图标 -->
			<tr style="text-align: center;" class="success">
				<td colspan="11" align="center"><span
					class="glyphicon glyphicon-plus btn btn-sm" aria-hidden="true"
					data-toggle="modal" data-target="#myAddModal"></span>
				</td>
			</tr>
		</table>
		<!-- 删除信息提示 -->
		<div id="deleteMsg" class="alert alert-success alert-dismissable" style="display:none;height:40px" >
					删除成功！
			 <button type="button" class="close" data-dismiss="alert" 
					  aria-hidden="true">
					  &times;
			 </button>
		</div>
		<!-- 新增模态框 -->
		<!-- Modal 新增模态框 -->
		<div class="modal"  id="myAddModal" tabindex="-1" role="dialog" height="100%"
			aria-labelledby="myModalLabel" aria-hidden="true">
			<div class="modal-dialog" role="document">
				<div class="modal-content">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal" aria-label="Close">
							<span aria-hidden="true">&times;</span>
						</button>
						<h4 class="modal-title" id="myModalLabel">增加科目</h4>
					</div>
					<div class="modal-body">
					<form:form method="post" modelAttribute="examSubject" id="form1">
						<div class="form-grop">
							<table cellSpacing="0" cellPadding="0" class="table-bordered">
								<tr class="success">
									<td>
										<label for="examSubjectName" class="control-label">科目名</label>
									</td>
									<td>
										<form:input path="examSubjectName" class="form-control" type="text"/>
									</td>
								</tr>
								<tr class="success">
									<td>
										<label for="examSubjectDescription" class="control-label">科目描述</label>
									</td>
									<td>
										<form:input path="examSubjectDescription" class="form-control" type="text"/>
									</td>
								</tr>
								<tr class="success">
									<td>
										<label for="examSubjectTnumber" class="control-label">科目总题数</label>
									</td>
									<td>
										<form:input path="examSubjectTnumber" class="form-control" type="text"/>
									</td>
								</tr>
								<tr class="success">
									<td>
										<label for="examSubjectTotaltime" class="control-label">考试总时长</label>
									</td>
									<td>
										<form:input path="examSubjectTotaltime" class="form-control" type="text"/>
									</td>
								</tr>
								<tr class="success">
									<td>
										<label for="examSubjectSum" class="control-label">总分</label>
									</td>
									<td>
										<form:input path="examSubjectSum" class="form-control" type="text"/>
									</td>
								</tr>
								<tr class="success">
									<td>
										<label for="examSubjectFlag" class="control-label">是否开放</label>
									</td>
									<td>
										<form:radiobutton path="examSubjectFlag" required="required" value="1"/>开放中&nbsp;&nbsp;
										<form:radiobutton path="examSubjectFlag" required="required" checked="checked" value="0"/>未开放
									</td>
									
								</tr>
								<tr class="success">
									<td>
										<label for="examSubjectDone" class="control-label">是否考试完成</label>
									</td>
									<td>
										<form:radiobutton path="examSubjectDone" required="required" value="1" />考试完成
										<form:radiobutton path="examSubjectDone" required="required" checked="checked" value="0" />尚未考试
									</td>
								</tr>
							</table>
						</div>
						
					<div class="modal-footer">
						<button type="button" id="close" class="btn btn-default" data-dismiss="modal">关闭</button>
						<button type="button" id="submit" class="btn btn-primary">保存</button>
						<!-- 添加信息提示 -->
						<div id="sucMsg" class="alert alert-success alert-dismissable" style="display:none">
								 <button type="button" class="close" data-dismiss="alert" 
									  aria-hidden="true">
					 					 &times;
									 </button>
   								增加成功！
						</div>
						<div id="eroMsg" class="alert alert-warning alert-dismissable" style="display:none">
								<button type="button" class="close" data-dismiss="alert" 
									  aria-hidden="true">
					 					 &times;
									 </button>
   								增加失败！
						</div>
					</div>
					</form:form>
					</div>
				</div>
			</div>
		</div>
		<!-- 编辑模态框 -->
		<div class="modal fade" id="myEditModal" tabindex="-1" role="dialog" height="100%"
			aria-labelledby="myModalLabel">
			<div class="modal-dialog" role="document">
				<div class="modal-content">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal"
							aria-label="Close">
							<span aria-hidden="true">&times;</span>
						</button>
						<h4 class="modal-title" id="myModalLabel">编辑科目</h4>
					</div>
					<div class="modal-body">
					<form:form method="post" modelAttribute="examSubject" id="form1">
						<div class="form-grop">
							<table cellSpacing="0" cellPadding="0" class="table-bordered">
								<form:input type="hidden" path="examSubjectId" id="examSubjectId1"/>
								<tr class="success">
									<td>
										<label for="examSubjectName" class="control-label">科目名</label>
									</td>
									<td>
										<form:input path="examSubjectName" id="examSubjectName1" class="form-control" type="text"/>
									</td>
								</tr>
								<tr class="success">
									<td>
										<label for="examSubjectDescription" class="control-label">科目描述</label>
									</td>
									<td>
										<form:input path="examSubjectDescription" id="examSubjectDescription1" class="form-control" type="text"/>
									</td>
								</tr>
								<tr class="success">
									<td>
										<label for="examSubjectTnumber" class="control-label">科目总题数</label>
									</td>
									<td>
										<form:input path="examSubjectTnumber" id="examSubjectTnumber1" class="form-control" type="text"/>
									</td>
								</tr>
								<tr class="success">
									<td>
										<label for="examSubjectTotaltime" class="control-label">考试总时长</label>
									</td>
									<td>
										<form:input path="examSubjectTotaltime" id="examSubjectTotaltime1" class="form-control" type="text"/>
									</td>
								</tr>
								<tr class="success">
									<td>
										<label for="examSubjectSum" class="control-label">总分</label>
									</td>
									<td>
										<form:input path="examSubjectSum" id="examSubjectSum1" class="form-control" type="text"/>
									</td>
								</tr>
								<tr class="success">
									<td>
										<label for="examSubjectFlag" class="control-label">是否开放</label>
									</td>
									<td id="radioFlag">
										<form:radiobutton path="examSubjectFlag" required="required" id="falg1"  value="1"/>开卷&nbsp;&nbsp;
										<form:radiobutton path="examSubjectFlag" required="required" id="falg2" checked="checked" value="0"/>闭卷
									</td>
									
								</tr>
								<tr class="success">
									<td>
										<label for="examSubjectDone" class="control-label">是否完成考试</label>
									</td>
									<td>
										<form:input path="examSubjectDone" id="examSubjectDone2" cssClass="form-control" type="text"/>
									</td>
								</tr>
							</table>
						</div>
					<div class="modal-footer">
						<button type="button" id="close" class="btn btn-default" data-dismiss="modal">关闭</button>
						<button type="button" id="submit1" class="btn btn-primary">保存</button>
						<!-- 添加信息提示 -->
						<div id="editSucMsg" class="alert alert-success alert-dismissable" style="display:none">
								 <button type="button" class="close" data-dismiss="alert" 
									  aria-hidden="true">
					 					 &times;
									 </button>
   								编辑成功！
						</div>
						<div id="editEroMsg" class="alert alert-warning alert-dismissable" style="display:none">
								<button type="button" class="close" data-dismiss="alert" 
									  aria-hidden="true">
					 					 &times;
									 </button>
   								编辑失败！
						</div>
					</div>
					</form:form>
					</div>
				</div>
			</div>
		</div>

</body>
</html>